package com.itheima.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Mapper.EmpExprMapper;
import com.itheima.Mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

    /***
     * 分页查询
     * @param
     * @return
     */


    public PageResult page(EmpQueryParam empQueryParam) {
        //1. 设置PageHelper分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2. 执行查询
        List<Emp> empList = empMapper.list(empQueryParam);
        //3. 封装分页结果
        Page<Emp> p = (Page<Emp>)empList;
        return new PageResult(p.getTotal(), p.getResult());
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        try {
            //1.补全基础属性
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            // 设置默认密码
            if (emp.getPassword() == null || emp.getPassword().isEmpty()) {
                emp.setPassword("123456"); // 默认密码
            }
            //2.保存员工基本信息
            empMapper.insert(emp);
     //       int i = 1/0;

            //3. 保存员工的工作经历信息 - 批量
            Integer empId = emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){

                exprList.forEach(a -> a.setEmpId(empId));
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }



    }

    //查找所有员工
    @Override
    public List<Emp> list() {
        return empMapper.findlist();
    }

    //根据id批量删除员工
    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        //根据ID删除一个
        empExprMapper.deleteByEmpIds(ids);
        empMapper.deleteByIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        //1根据ID更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);


      //2根据员工ID删除员工的工作经历信息 【删除老的】

        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        //3. 新增员工工作经历数据
        Integer empId=emp.getId();
        List<EmpExpr> exprList=emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr->expr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if (empLogin != null) {
            //1.生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id",empLogin.getId());
            dataMap.put("username",empLogin.getUsername());

            String jwt= JwtUtils.generateJwt(dataMap);

            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);//new 对象封装
            return loginInfo;
        }
        return null;



    }


}


//非PageHeler的分页代码
/*    public PageResult page(Integer page, Integer pageSize) {
        //1、查询总数量
        Long total = empMapper.count();

        //2、计算查询的起始索引
        Integer start = (page-1)*pageSize;

        //3、执行查询
        List<Emp> list = empMapper.list(start,pageSize);

        //4、封装PageResult对象
        return new PageResult(total,list);

    }*/